<%@ page pageEncoding="UTF-8"%>
<%@ include file="/common/common.jsp"%>
<%
	//查询床位占用率
	String yydm =  input.getDesc("yydm","01");//医院代码	
	String ksdm = input.getDesc("ksdm","-1"); //传入科室代码代表该科室主任
	String date = input.getDesc("start_date","2018-01-11");//开始时间
	String endDa = input.getDesc("end_date","2018-01-11");//结束时间

	SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
	SimpleDateFormat sdf2 = new SimpleDateFormat("yyyyMMdd");
	String nowDate = sdf2.format(sdf.parse(date));
	String nowEndDa = sdf2.format(sdf.parse(endDa));
	
	String startDate = nowDate+"00:00:00";
	String endDate = nowEndDa+"23:59:59";
	//返回参数
	DecimalFormat df = new DecimalFormat("######0.00"); 
	Message msg = new Message();
	//当前使用数量
	double currentCount = 0;
	double sumCount = 1;
	String sqlWhere = "";
	if(!yydm.equals("-1"))
	sqlWhere += " and a.ksdm in (select id  from his_dept where yydm ='"+yydm+"') " ;
	String hql4Where = " ";
	if(!ksdm.equals("-1")){
		sqlWhere += " and a.ksdm in(" + ksdm + ")";
	
		hql4Where += " group by ksdm";
		//在院人数
		String hql3 = "select new map(count(a) as count ,a.ksdm as ksdm) from ZY_BRSYK a where brzt !=9 and rqrq != '' and cwdm !='' and cwdm is not null and (( cyrq >= '" + endDate + "' and rqrq <= '" + endDate + "' ) or (cyrq is null and rqrq <= '" + endDate + "')) "+ sqlWhere +" group by ksdm order by count(a) desc";
		List<Map> result3 = QBuilder.db("basedb").queryHql(hql3);
		
		//查询床位总数
		String hql4 = "select new map(count(ksdm) as count ,ksdm) from ZY_BCDMK a where bzlx = 0 and jlzt = 0 "  + sqlWhere +hql4Where; 
		List<Map> result4 = QBuilder.db("basedb").queryHql(hql4);
		
		currentCount = result3 != null && result3.size() >0 && result3.get(0).get("count") != null ? Double.parseDouble( result3.get(0).get("count").toString()) : 0 ;
		String str =  result4 != null && result4.size() >0 && result4.get(0).get("count") != null ? result4.get(0).get("count").toString() : "1" ;
		sumCount = Double.parseDouble(str);
		sumCount = sumCount == 0 ? 1 : sumCount;
	}else{
		//在院人数
		
		String hql3 = "select new map(count(a) as count ,a.ksdm as ksdm) from ZY_BRSYK a where brzt !=9 and rqrq != '' and cwdm !='' and cwdm is not null and (( cyrq >= '" + endDate + "' and rqrq <= '" + endDate + "' ) or (cyrq is null and rqrq <= '" + endDate + "')) "+ sqlWhere +" group by ksdm order by count(a) desc";
			
		List<Map> result3 = QBuilder.db("basedb").queryHql(hql3);
			
		for(Map map : result3){
			double tempCount = map.get("count") != null ? Double.parseDouble( map.get("count").toString()) : 0 ;
		currentCount += tempCount;
		}
		System.out.println(currentCount);
		/*
		//查询床位总数
		String hql4 = "select new map(count(*) as count ) from ZY_BCDMK a where bzlx = 0 and jlzt = 0 " + sqlWhere  ; 
		List<Map> result4 = QBuilder.db("basedb").queryHql(hql4);
		//currentCount = result3 != null && result3.size() >0 && result3.get(0).get("count") != null ? Double.parseDouble( result3.get(0).get("count").toString()) : 0 ;
	
		
		String str =  result4 != null && result4.size() >0 && result4.get(0).get("count") != null ? result4.get(0).get("count").toString() : "1" ;
		
		sumCount = Double.parseDouble(str);
		sumCount = sumCount == 0 ? 1 : sumCount; 
		*/
		if("01".equals(yydm)){
			sumCount = 760;//安镇
		}else if("02".equals(yydm)){
			sumCount = 266;//广瑞
		}else{
			sumCount = 1026;//总数
		}
	
	}
	Map<String,Object> m2 = new HashMap<String,Object>();
	m2.put("sumCount",df.format(currentCount/sumCount*100));
	
	Map<String,Object> m1 = new HashMap<String,Object>();
	m1.put("data",m2);
	msg.put("errcode",0);
	msg.put("zy_baduserate",m1);

	
	
	
	out.println(msg);
%>